package com.marketmachine.android.br;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;

public class DrivingDirectionWindow extends ListActivity {
	public static final int MIL = 1000000;
	public void onCreate (Bundle b) {
		super.onCreate (b);
		setContentView (R.layout.dir_layout);
		/*Log.d ("DEBUG_NOTIFICATION", getIntent ().getStringExtra ("start_coords"));
		Log.d ("DEBUG_NOTIFICATION", getIntent ().getStringExtra ("end_coords"));
		String [] latlong1 = getIntent ().getStringExtra ("start_coords").split (",");
		String [] latlong2 = getIntent ().getStringExtra ("end_coords").split (",");
		latlong2[0] = latlong2[0].trim ();
		latlong2[1] = latlong2[1].trim ();
		int lat1 = (int)(Math.round(Double.parseDouble (latlong1[0].trim ()) * MIL));
		int lon1 = (int)(Math.round(Double.parseDouble (latlong1[1].trim ()) * MIL));
		int lat2 = (int)(Math.round(Double.parseDouble (latlong2[0].trim ()) * MIL));
		int lon2 = (int)(Math.round(Double.parseDouble (latlong2[1].trim ()) * MIL));
		Log.d ("DEBUG_NOTIFICATION", "" + lat1);
		Log.d ("DEBUG_NOTIFICATION", "" + lon1);
		Log.d ("DEBUG_NOTIFICATION", "" + lat2);
		Log.d ("DEBUG_NOTIFICATION", "" + lon2);
		MapPoint start = new MapPoint (lat1, lon1);
		MapPoint end = new MapPoint (lat2, lon2);
		DrivingDirection dd = new DrivingDirection (start, "", end, "");
		if (dd != null) {
			while (dd.isComplete () == false) {

		}
			//if (dd.getState () == DrivingDirection.SUCCESS_STATUS) {
			Log.d ("DEBUG_NOTIFICATION", "BEFORE getTurns");
			String [] directions = dd.getTurns ();

			if (directions != null) {
				Log.d ("DEBUG_NOTIFICATION", "BEFORE SET ARRAYADAPTER");
				ArrayAdapter <String> directions_adapter = new ArrayAdapter <String> (this, R.layout.locs_row, directions);
				setListAdapter (directions_adapter);
			} else {
				Log.d ("DEBUG_NOTIFICATION", "NULL DIRECTIONS");
			}
			} else {
			Log.d ("DEBUG_NOTIFICATION", "ERROR FINDING DIRECTIONS");
		}
		}*/
		String start_addr = getIntent ().getStringExtra ("start_addr");
		String end_addr = getIntent ().getStringExtra ("end_addr");
		ArrayList<String> real_dirs = getDirections (start_addr, end_addr);
		ArrayAdapter <String> directions_adapter = new ArrayAdapter <String> (this, R.layout.locs_row, real_dirs);
		setListAdapter (directions_adapter);
	} 




	public static ArrayList <String> getDirections (String start_addr, String end_addr) {
		String form_start = "";
		String form_end = "";
		String [] start_comps = start_addr.split (" ");
		for (int x = 0; x < start_comps.length; x++) {
			if (x == 0) {
				form_start = form_start + start_comps[x];
			} else {
				form_start = form_start + "+" + start_comps[x];
			}
		}
		String [] end_comps = end_addr.split (" ");
		for (int x = 0; x < end_comps.length; x++) {
			if (x == 0) {
				form_end = form_end + end_comps[x];
			} else {
				form_end = form_end + "+" + end_comps[x];
			}
		}
		String pageReq = "http://www.mapquest.com/maps/" + form_start + "/" + form_end + "/";
		Log. d("PAGEREQUEST", pageReq);
		try {
			URL dir_page = new URL (pageReq);
			URLConnection dir_conn = dir_page.openConnection ();
			BufferedReader directions_reader = new BufferedReader (new InputStreamReader (dir_conn.getInputStream()));
			String in;
			ArrayList <String> real_dirs = new ArrayList<String> ();
			while ((in = directions_reader.readLine ()) != null) {
				//Log.d ("DEBUG_NOTIFICATION", "ENTERED_WHILE");
				Log.d ("", in);
				if (in.contains ("narrative") == true) {
					Log.d ("DEBUG_NOTIFICATION", "CONTAINS NARRATIVE");
					String [] dirs = in.split ("narrative");
					for (int x = 0; x < dirs.length; x++) {
						real_dirs.add (dirs[x].substring(3, dirs[x].indexOf ("\",\"roadSign")));
					}
					break;

				}
			}
			if (real_dirs.isEmpty () == false) {
				return real_dirs;
			} else {
				Log.d ("PAGE_ADDR", pageReq);
				Log.d ("START_ADDR", start_addr);
				Log.d ("END_ADDR", end_addr);
				String new_start = "";
				//form_start = form_start.replaceAll("+", "<");
				String [] comps = start_addr.split (" ");
				for (int x = 1; x < comps.length; x++){
					if (x == (comps.length - 1)) {
						new_start = new_start + comps[x];
					} else {
						new_start = new_start + comps[x] + " ";
					}
				}
				//return getDirections (new_start, end_addr);
				return real_dirs;
			}
		}catch (Exception e) {
			Log.d ("DEBUG_NOTIFICATION", "EXCEPTION THROWN " + e.getLocalizedMessage ());
			return null;
		}
	}
}
